package com.project.webapp.action;

import java.io.ByteArrayInputStream;
import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.project.common.util.RandomNumUtil;

/**
 * 随机码实作
 * @author Sn
 *
 */
public class GetRandomNumAction extends ActionSupport 
{
    private static final long serialVersionUID = 1L;
    private ByteArrayInputStream inputStream;

    public String execute() throws Exception 
    {
        RandomNumUtil randomNumUtil = RandomNumUtil.Instance();
        this.setInputStream(randomNumUtil.getImage());
        ActionContext.getContext().getSession().put("validateCode", randomNumUtil.getString());
        return SUCCESS;
    }
    
    //检核随机验证码
    public void getRandomCode() throws Exception
    {
    	String strFlag = "N";
    	//画面输入randomCode
    	String parmCode = (String)ServletActionContext.getRequest().getParameter("randomCode");
    	//Session 中保存randomCode
    	String randomCode = (String)ActionContext.getContext().getSession().get("validateCode");
    	
        PrintWriter writer = null;
        writer = ServletActionContext.getResponse().getWriter();
        
        if (parmCode.equals(randomCode)) 
        {
        	strFlag = "Y";
        }else 
        {
        	strFlag = "N";
        }
        
        if(!"".equals(strFlag))
        {
            writer.print(strFlag);
        }
    }
    
    public ByteArrayInputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(ByteArrayInputStream inputStream) {
        this.inputStream = inputStream;
    }
}
